package flst.ipiweb.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flst.ipiweb.transverse.IPIException;

public class ServiceDate {
	
	public static SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
	public static SimpleDateFormat formatterStat = new SimpleDateFormat("MM/dd/yyyy");
	 
	public Date convertStringToDate(String laDate) throws IPIException
	{
		
		try {
			return formatter.parse(laDate);
		} catch (ParseException e) {
			throw new IPIException("Echec conversion de la date");
		}
		
	}
	
	public String convertDateToString(Date laDate)
	{
		return formatter.format(laDate);
	}
	
	public java.sql.Date convertDateToSqlDate(java.util.Date laDate)
	{
		return new java.sql.Date(laDate.getTime());
	}
	
	public java.util.Date convertSqlDateToDate(java.sql.Date laDate)
	{
		return new java.util.Date(laDate.getTime());
	}
	
	public java.util.Date getFirstDateOfMonth()
	{
		Date dateDuJour = new java.util.Date();
		
		Calendar c = Calendar.getInstance();
		c.setTime(dateDuJour);
		
		// on se place au premier jour du mois en cours
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		Date firstDay = c.getTime();
		
		return firstDay;
	}
	
	public java.util.Date getLastDateOfMonth()
	{
		Date dateDuJour = new java.util.Date();
		
		Calendar c = Calendar.getInstance();
		c.setTime(dateDuJour);
		
		// on se place au dernier jour du mois en cours
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date lastDay = c.getTime();
		
		return lastDay;
	}
	
	public java.util.Date getFirstDateOfYear()
	{
		Date dateDuJour = new java.util.Date();
		
		Calendar c = Calendar.getInstance();
		c.setTime(dateDuJour);
		
		// on se place au premier jour de l'année en cours
		c.set(Calendar.DAY_OF_YEAR, c.getActualMinimum(Calendar.DAY_OF_YEAR));
		Date firstDay = c.getTime();
		
		return firstDay;
	}
	
	public java.util.Date getLastDateOfYear()
	{
		Date dateDuJour = new java.util.Date();
		
		Calendar c = Calendar.getInstance();
		c.setTime(dateDuJour);
		
		// on se place au dernier jour de l'année en cours
		c.set(Calendar.DAY_OF_YEAR, c.getActualMaximum(Calendar.DAY_OF_YEAR));
		Date lastDay = c.getTime();
		
		return lastDay;
	}
	
	public String convertDateForStat(String laDate) throws IPIException
	{
		try {
			return formatterStat.format(formatter.parse(laDate));
		} catch (ParseException e) {
			throw new IPIException("Echec conversion de la date");
		}
	}
}
